//
//  LHQDestinationtripViewController.m
//  God_is_a_Girl
//
//  Created by dllo on 15/9/28.
//  Copyright (c) 2015年 呸. All rights reserved.
//

#import "LHQDestinationtripViewController.h"
#import "LHQDestinationTripModel.h"
#import "LHQDestinationtripTableViewCell.h"
#import <AFNetworking.h>
#import "LHQWebViewController.h"
@interface LHQDestinationtripViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, retain)UITableView *tableview;

@property (nonatomic, retain)UIView *myView;

@property (nonatomic, assign)BOOL i;

@property (nonatomic, retain)NSMutableArray *ALLarr;

@property (nonatomic, retain) UIActivityIndicatorView *activity;


@end

@implementation LHQDestinationtripViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(downButton:)];
    [self tabelViewSubView];
    [self DataHandel];
    self.myView = [[UIView alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 6 * 5, 0, self.view.frame.size.width / 6, 0)];
    self.myView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:self.myView];
    
    
    self.activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    self.activity.color = [UIColor redColor];
    self.activity.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 64 - 49);
    [self.view addSubview:self.activity];
    [self.activity startAnimating];
    
    

}
- (void)DataHandel
{
    NSString *str = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%@/%@/pois/all/?start=0&count=20&sort=default&shift=false&latitude=0&longitude=0",self.typeStr, self.idStr];
    NSLog(@"%@",str);
    self.ALLarr = [[NSMutableArray alloc]init];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSArray *arr = [responseObject objectForKey:@"items"];
        for (NSDictionary *dic in arr) {
            
            LHQDestinationTripModel *model = [[LHQDestinationTripModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.ALLarr addObject:model];
        }
        NSLog(@"%ld", self.ALLarr.count);
        [self.tableview reloadData];
        [self.activity stopAnimating];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
    
}




- (void)tabelViewSubView
{
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableview.backgroundColor = [UIColor colorWithRed:255 / 255.0 green:246 / 255.0 blue:233 / 255.0 alpha:1];
    [self.view addSubview:self.tableview];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.ALLarr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    LHQDestinationtripTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    if (cell == nil) {
        cell = [[LHQDestinationtripTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuse"];
    }
    cell.backgroundColor = [UIColor colorWithRed:255 / 255.0 green:246 / 255.0 blue:233 / 255.0 alpha:1];
     LHQDestinationTripModel *model = [self.ALLarr objectAtIndex:indexPath.row];
    cell.model = model;
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return (self.view.frame.size.height - 64) / 4;
}


- (void)downButton:(UIBarButtonItem *)button
{
    
    if (self.i == 1) {
        
        [UIView animateWithDuration:0.3 animations:^{
            
            self.myView.frame = CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width / 6, 0);
            for (UIButton *button1 in self.myView.subviews) {
                [button1 removeFromSuperview];
            }
            
        }];
        self.i = 0;
        
        
    }
    else{
        
        [UIView animateWithDuration:0.5 animations:^{
            
            self.myView.frame = CGRectMake(self.view.frame.size.width / 6 * 5, 0, self.view.frame.size.width / 6, 240);
            NSArray * arr = @[@"全部",@"住宿",@"餐厅",@"娱乐",@"购物",@"景点"];
            for (int j = 0; j < 6; j++) {
                UIButton *downbuttons = [UIButton buttonWithType:UIButtonTypeCustom];
                downbuttons.frame = CGRectMake(0, j * 40, self.view.frame.size.width / 6, 40);
                downbuttons.backgroundColor = [UIColor lightGrayColor];
                [self.myView addSubview:downbuttons];
                [downbuttons setTitle:arr[j] forState:UIControlStateNormal];
                
                [downbuttons addTarget:self action:@selector(TZbutton:) forControlEvents:UIControlEventTouchUpInside];
                
                downbuttons.tag = 100+j;
            }
            
        }];
        self.i = 1;
        
    }
    
}


- (void)TZbutton:(UIButton *)button
{
    
    self.activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    self.activity.color = [UIColor redColor];
    self.activity.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 64 - 49);
    [self.view addSubview:self.activity];
    [self.activity startAnimating];
    
    
    NSLog(@"%ld",button.tag);
    [UIView animateWithDuration:0.3 animations:^{
        
        self.myView.frame = CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width / 6, 0);
        for (UIButton *button1 in self.myView.subviews) {
            [button1 removeFromSuperview];
        }
        
    }];
    self.i = 0;
    
    NSArray *arr = @[@"all",@"hotel",@"restaurant",@"experience",@"mall",@"sights"];
    
    [self.ALLarr removeAllObjects];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    NSString *string = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%@/%@/pois/%@/?start=0&count=20&sort=default&shift=false&latitude=0&longitude=0",self.typeStr, self.idStr, arr[button.tag - 100]];
    NSLog(@"%@",string);
    
    [manager GET:string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSArray *arr = [responseObject objectForKey:@"items"];
        for (NSDictionary *dic in arr) {
            
            LHQDestinationTripModel *model = [[LHQDestinationTripModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.ALLarr addObject:model];
        }
        NSLog(@"%ld", self.ALLarr.count);
        [self.tableview reloadData];
        [self.activity stopAnimating];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
    
    
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    LHQWebViewController *web = [[LHQWebViewController alloc]init];
    
   web.str = [[self.ALLarr objectAtIndex:indexPath.row] slug_url];
    
    [self.navigationController pushViewController:web animated:YES];

}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
